package com.fsxgt.datagrid.console.controller;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSONObject;
import com.fsxgt.datagrid.console.dao.FlowExecLogMapper;
import com.fsxgt.datagrid.console.exception.RunningException;
import com.fsxgt.datagrid.core.utils.ReturnT;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;



@Api(tags = "运行统计", value = "StatisticsController")
@RequestMapping(produces = "application/json; charset=UTF-8")
@RestController
public class StatisticsController extends BaseController {


	@Autowired
	private FlowExecLogMapper flowExecLogMapper;


	@ApiOperation(hidden = true, value = "调用executor", notes = "")
	@RequestMapping(path = "/statistics/statusCount", method = RequestMethod.POST)
	public Object statusCount(@RequestBody JSONObject param, HttpServletRequest request) throws RunningException {
		
		Calendar c1 = Calendar.getInstance();
		c1.add(Calendar.HOUR_OF_DAY, -48);
		
		List<Map> list=flowExecLogMapper.findStatusCountList(c1.getTime());
		
		List result=new ArrayList();
		Map succMap=new HashMap();
		succMap.put("name", "成功");
		succMap.put("data", new ArrayList());
		succMap.put("xAxisData", new ArrayList());
		
		Map errorMap=new HashMap();
		errorMap.put("name", "失败");
		errorMap.put("data", new ArrayList());
		errorMap.put("xAxisData", new ArrayList());
		
		result.add(succMap);
		result.add(errorMap);
		
		list.forEach(m->{
			((List)succMap.get("data")).add(m.get("success"));
			((List)succMap.get("xAxisData")).add(m.get("dateTime"));
			
			((List)errorMap.get("data")).add(m.get("error"));
			((List)errorMap.get("xAxisData")).add(m.get("dateTime"));
		});
		
		return new ReturnT(result);
		
	}
	

}